package com.example.reference_book.controller;

import com.example.reference_book.entity.Dept;
import com.example.reference_book.entity.DeptInfo;
import com.example.reference_book.service.DeptService;
import com.example.reference_book.util.DeptSearchUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 专业表(Dept)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:18:32
 */
@RestController
@RequestMapping("dept")
public class DeptController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    private DeptSearchUtil searchUtil = DeptSearchUtil.getSearchUtil();

    @GetMapping("/searchByKey/{type}/{key}")
    public ResponseEntity<List<DeptInfo>> searchByKey(@PathVariable("type") Integer type, @PathVariable("key") String key){
        return ResponseEntity.ok(this.searchUtil.searchByKey(type,key));
    }

    /**
     * 查询所有数据
     */
    @PostMapping("/selectAll")
    public ResponseEntity<List<DeptInfo>> selectAll() {
        List<DeptInfo> list = this.deptService.selectAll();
        searchUtil.init(list);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/selectBySchoolId")
    public ResponseEntity<List<Dept>> selectBySchoolId(int id) {
        return ResponseEntity.ok(this.deptService.selectBySchoolId(id));
    }

    /**
     * 分页查询
     *
     * @param dept 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Dept>> queryByPage(Dept dept, PageRequest pageRequest) {
        return ResponseEntity.ok(this.deptService.queryByPage(dept, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Dept> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.deptService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dept 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<Dept> add(Dept dept) {
        return ResponseEntity.ok(this.deptService.insert(dept));
    }

    /**
     * 编辑数据
     *
     * @param dept 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseEntity<Dept> edit(Dept dept) {
        return ResponseEntity.ok(this.deptService.update(dept));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.deptService.deleteById(id));
    }

}

